home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / Miro_Installer.exe / Miro_Downloader.exe / uu.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-01-10  |  4.0 KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Implementation of the UUencode and UUdecode functions.
  5.  
  6. encode(in_file, out_file [,name, mode])
  7. decode(in_file [, out_file, mode])
  8. '''
  9. import binascii
  10. import os
  11. import sys
  12. __all__ = [
  13.     'Error',
  14.     'encode',
  15.     'decode']
  16.  
  17. class Error(Exception):
  18.     pass
  19.  
  20.  
  21. def encode(in_file, out_file, name = None, mode = None):
  22.     '''Uuencode file'''
  23.     if in_file == '-':
  24.         in_file = sys.stdin
  25.     elif isinstance(in_file, basestring):
  26.         if name is None:
  27.             name = os.path.basename(in_file)
  28.         
  29.         if mode is None:
  30.             
  31.             try:
  32.                 mode = os.stat(in_file).st_mode
  33.             except AttributeError:
  34.                 pass
  35.             except:
  36.                 None<EXCEPTION MATCH>AttributeError
  37.             
  38.  
  39.         None<EXCEPTION MATCH>AttributeError
  40.         in_file = open(in_file, 'rb')
  41.     
  42.     if out_file == '-':
  43.         out_file = sys.stdout
  44.     elif isinstance(out_file, basestring):
  45.         out_file = open(out_file, 'w')
  46.     
  47.     if name is None:
  48.         name = '-'
  49.     
  50.     if mode is None:
  51.         mode = 438
  52.     
  53.     out_file.write('begin %o %s\n' % (mode & 511, name))
  54.     data = in_file.read(45)
  55.     while len(data) > 0:
  56.         out_file.write(binascii.b2a_uu(data))
  57.         data = in_file.read(45)
  58.     out_file.write(' \nend\n')
  59.  
  60.  
  61. def decode(in_file, out_file = None, mode = None, quiet = 0):
  62.     '''Decode uuencoded file'''
  63.     if in_file == '-':
  64.         in_file = sys.stdin
  65.     elif isinstance(in_file, basestring):
  66.         in_file = open(in_file)
  67.     
  68.     while True:
  69.         hdr = in_file.readline()
  70.         if not hdr:
  71.             raise Error('No valid begin line found in input file')
  72.         
  73.         if not hdr.startswith('begin'):
  74.             continue
  75.         
  76.         hdrfields = hdr.split(' ', 2)
  77.         if len(hdrfields) == 3 and hdrfields[0] == 'begin':
  78.             
  79.             try:
  80.                 int(hdrfields[1], 8)
  81.             except ValueError:
  82.                 pass
  83.             except:
  84.                 None<EXCEPTION MATCH>ValueError
  85.             
  86.  
  87.         None<EXCEPTION MATCH>ValueError
  88.     if out_file is None:
  89.         out_file = hdrfields[2].rstrip()
  90.         if os.path.exists(out_file):
  91.             raise Error('Cannot overwrite existing file: %s' % out_file)
  92.         
  93.     
  94.     if mode is None:
  95.         mode = int(hdrfields[1], 8)
  96.     
  97.     if out_file == '-':
  98.         out_file = sys.stdout
  99.     elif isinstance(out_file, basestring):
  100.         fp = open(out_file, 'wb')
  101.         
  102.         try:
  103.             os.path.chmod(out_file, mode)
  104.         except AttributeError:
  105.             pass
  106.  
  107.         out_file = fp
  108.     
  109.     s = in_file.readline()
  110.     while s and s.strip() != 'end':
  111.         
  112.         try:
  113.             data = binascii.a2b_uu(s)
  114.         except binascii.Error:
  115.             v = None
  116.             nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) // 3
  117.             data = binascii.a2b_uu(s[:nbytes])
  118.             if not quiet:
  119.                 sys.stderr.write('Warning: %s\n' % v)
  120.             
  121.         except:
  122.             quiet
  123.  
  124.         out_file.write(data)
  125.         s = in_file.readline()
  126.     if not s:
  127.         raise Error('Truncated input file')
  128.     
  129.  
  130.  
  131. def test():
  132.     '''uuencode/uudecode main program'''
  133.     import optparse
  134.     parser = optparse.OptionParser(usage = 'usage: %prog [-d] [-t] [input [output]]')
  135.     parser.add_option('-d', '--decode', dest = 'decode', help = 'Decode (instead of encode)?', default = False, action = 'store_true')
  136.     parser.add_option('-t', '--text', dest = 'text', help = 'data is text, encoded format unix-compatible text?', default = False, action = 'store_true')
  137.     (options, args) = parser.parse_args()
  138.     if len(args) > 2:
  139.         parser.error('incorrect number of arguments')
  140.         sys.exit(1)
  141.     
  142.     input = sys.stdin
  143.     output = sys.stdout
  144.     if len(args) > 0:
  145.         input = args[0]
  146.     
  147.     if len(args) > 1:
  148.         output = args[1]
  149.     
  150.     if options.decode:
  151.         if options.text:
  152.             if isinstance(output, basestring):
  153.                 output = open(output, 'w')
  154.             else:
  155.                 print sys.argv[0], ': cannot do -t to stdout'
  156.                 sys.exit(1)
  157.         
  158.         decode(input, output)
  159.     elif options.text:
  160.         if isinstance(input, basestring):
  161.             input = open(input, 'r')
  162.         else:
  163.             print sys.argv[0], ': cannot do -t from stdin'
  164.             sys.exit(1)
  165.     
  166.     encode(input, output)
  167.  
  168. if __name__ == '__main__':
  169.     test()
  170.  
  171.